import { login, logout, getInfo } from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  getUserId,
  setUserId,
  removeUserId,
  getName,
  setName,
  removeName
} from '@/utils/auth'
import router, { resetRouter } from '@/router'
import { userLoginApi, getUserInfoApi } from '@/api/login'
const state = {
  token: getToken() || '',
  userId: getUserId() || '',
  name: getName() || '',
  userInfo: []
}

const mutations = {
  // 存储token
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  // 存储id
  SET_ID(state, id) {
    state.userId = id;
    setUserId(id); // 设置id
  },
  // 存储name
  SET_NAME(state, name) {
    state.name = name;
    setName(name); // 设置name
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  // 路由信息
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo;
  },
  // 删除token
  REMOVE_TOKEN(state) {
    state.token = null;
    removeToken();
  },
  // 删除id
  REMOVE_ID(state) {
    state.userId = null;
    removeUserId();
  },
  // 删除name
  REMOVE_NAME(state) {
    state.name = "";
    removeName();
  },
  // 删除userInfo
  REMOVE_USER_INFO(state, userInfo) {
    state.userInfo = userInfo;
  },
  // 免登录
  mobileLogin(state, token) {
    console.log(token);
    commit("SET_TOKEN", token);
    setToken(token)
    router.push("/");
  },
};

const actions = {
  // user login
  // login({ commit }, userInfo) {
  //   const { username, password } = userInfo
  //   return new Promise((resolve, reject) => {
  //     login({ username: username.trim(), password: password }).then(response => {
  //       const { data } = response
  //       commit('SET_TOKEN', data.token)
  //       setToken(data.token)
  //       resolve()
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },
  // 登录
  async login(context, data) {
    const res = await userLoginApi(data)
    console.log(res)
    if (res.code === 1) {
      context.commit('SET_TOKEN', res.data.token)
      context.commit('SET_ID', res.data.id)
      context.commit('SET_NAME', res.data.loginName)
      setToken(res.data.token)
      setUserId(res.data.id)
      setName(res.data.loginName)
      return res
    } else {
      // 处理失败情况
      console.error('登录失败:', res.msg)
      Message.error(res.msg || '登录失败')
      return Promise.reject(res)
    }
  },

  // 获取用户信息
  async getUserInfo(context) {
    const res = await getUserInfoApi(context.state.userId)
    if (res.code === 1) {
      const userInfo = res.data.map((item) => item.url)
      context.commit('SET_USER_INFO', userInfo)
      console.log(userInfo)
      // 在提交mutation之前打印
      console.log('即将提交到mutation的userInfo:', userInfo)
      return userInfo // 返回用户信息
      // 在提交mutation之后立即获取state验证
    } else {
      // console.log(res);
      Message.error(res.msg)
      return [] // 确保在失败时返回空数组
    }
  },
  // 退出登录
  logout(context) {
    // 清空信息 token
    context.commit('REMOVE_TOKEN')
    // 清空 userId
    context.commit('REMOVE_ID')
    // 清空个人信息
    context.commit('REMOVE_USER_INFO', [])
    // 清空 name
    context.commit('REMOVE_NAME')
    // 重置路由
    resetRouter()
    // 去设置权限模块下路由为初始状态
    // 调用根级的mutations或者action都是挂载全局的 所以可以直接调用
    context.commit('permission/setRoutes', [], { root: true })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
